import 'package:flutter_test/flutter_test.dart';
import 'package:plate_lpr/plate_lpr.dart';
import 'package:flutter/services.dart';

void main() {
  const MethodChannel channel = MethodChannel('yk.flutter.platelpr');

  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'getPlatformVersion':
          return 'Android 13';
        case 'startPlateLpr':
          // 模拟成功识别结果
          return {
            'success': true,
            'plateNumber': '京A12345',
            'confidence': 0.95,
            'imagePath':
                '/storage/emulated/0/Android/data/com.example/files/Pictures/PlateLpr/IMG_20240101_120000.jpg',
            'isNoPlate': false,
          };
        default:
          throw PlatformException(
            code: 'UNIMPLEMENTED',
            message: '方法 ${methodCall.method} 未实现',
          );
      }
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  group('PlateLpr 插件测试', () {
    test('获取平台版本', () async {
      expect(await PlateLpr.platformVersion, 'Android 13');
    });

    test('快速启动识别 - 成功场景', () async {
      final result = await PlateLpr.quickStart();

      expect(result.success, true);
      expect(result.plateNumber, '京A12345');
      expect(result.confidence, 0.95);
      expect(result.isNoPlate, false);
      expect(result.imagePath, isNotNull);
    });

    test('带按钮选项启动', () async {
      final result = await PlateLpr.startWithButtonOption(
        title: '测试车牌识别',
        buttonType: RecogButtonType.noPlate,
      );

      expect(result.success, true);
    });

    test('自定义配置启动', () async {
      final result = await PlateLpr.startRecognition(
        config: const PlateLprConfig(
          title: '自定义识别',
          recogButtonType: RecogButtonType.manualInput,
        ),
      );

      expect(result.success, true);
    });
  });

  group('PlateLprResult 测试', () {
    test('从Map创建结果对象', () {
      final map = {
        'success': true,
        'plateNumber': '沪B67890',
        'confidence': 0.88,
        'imagePath': '/test/path/image.jpg',
      };

      final result = PlateLprResult.fromMap(map);

      expect(result.success, true);
      expect(result.plateNumber, '沪B67890');
      expect(result.confidence, 0.88);
      expect(result.imagePath, '/test/path/image.jpg');
      expect(result.isNoPlate, false);
    });

    test('转换为Map', () {
      const result = PlateLprResult(
        success: true,
        plateNumber: '粤C11111',
        confidence: 0.92,
        imagePath: '/test/image.jpg',
      );

      final map = result.toMap();

      expect(map['success'], true);
      expect(map['plateNumber'], '粤C11111');
      expect(map['confidence'], 0.92);
      expect(map['imagePath'], '/test/image.jpg');
      expect(map['isNoPlate'], false);
    });

    test('无牌车结果', () {
      const result = PlateLprResult(
        success: true,
        plateNumber: 'noplate',
      );

      expect(result.success, true);
      expect(result.isNoPlate, true);
      expect(result.plateNumber, 'noplate');
    });

    test('失败结果', () {
      const result = PlateLprResult(
        success: false,
        error: '相机权限被拒绝',
      );

      expect(result.success, false);
      expect(result.error, '相机权限被拒绝');
    });
  });

  group('PlateLprConfig 测试', () {
    test('默认配置', () {
      const config = PlateLprConfig();

      expect(config.title, '车牌识别');
      expect(config.recogButtonType, RecogButtonType.hidden);
    });

    test('自定义配置', () {
      const config = PlateLprConfig(
        title: '自定义标题',
        recogButtonType: RecogButtonType.noPlate,
      );

      expect(config.title, '自定义标题');
      expect(config.recogButtonType, RecogButtonType.noPlate);
    });

    test('转换为Map', () {
      const config = PlateLprConfig(
        title: '测试标题',
        recogButtonType: RecogButtonType.manualInput,
      );

      final map = config.toMap();

      expect(map['title'], '测试标题');
      expect(map['recogButtonType'], RecogButtonType.manualInput.index);
    });
  });
}
